package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Reference
    public SetmealService setmealService;


    @GetMapping("/findById")
    public Result findById(@RequestParam("id") Integer setmealId){
        Setmeal setmeal = setmealService.findById(setmealId);
        if(StringUtils.isEmpty(setmeal)){
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
    }

    /**
     * 修改套餐
     *
     * @param setmeal
     * @param checkGroupIds
     * @return
     */
    @PostMapping("/edit")
    public Result updateSetmeal(@RequestBody Setmeal setmeal, @RequestParam List<Integer> checkGroupIds) {
        if (StringUtils.isEmpty(setmeal)) {
            return new Result(false, "你传入的数据有误，请正确传入需要编辑的数据");
        }
        try {
            boolean b = setmealService.updateSetmeal(setmeal, checkGroupIds);
            return new Result(true, "编辑套餐成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "编辑套餐失败");
        }
    }



    @GetMapping("/findCheckGroupIdsBySetmealId")
    public Result findCheckGroupIdsBySetmealId(@RequestParam("id") Integer setmealId){
        List<Integer> checkGroupIds = setmealService.findCheckGroupIdsBySetmealId(setmealId);
        if(CollectionUtils.isEmpty(checkGroupIds)){
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroupIds);
    }

    @PostMapping("/add")
    public Result addSetmeal(@RequestBody Setmeal setmeal, @RequestParam("checkgroupIds") List<Integer> checkgroupIds){
        if (StringUtils.isEmpty(setmeal)) {
            return new Result(false, "你传入的数据有误，请正确传入需要编辑的数据");
        }
        return setmealService.addSetmeal(setmeal, checkgroupIds);
    }

    @PostMapping("/delete/{id}")
    public Result deleteSetmeal(@PathVariable("id") Integer id, @RequestParam("checkgroupIds") List<Integer> checkgroupIds) {
        //1.先查询如果没有直接返回错误

        //2.进行删除
        return setmealService.deleteSetmeal(id, checkgroupIds);
    }
}
